const models = require('../db/mysqldb/index')
const logger = require('../utils/logger')

class ChatGroupInfoModel {
  /**
   * @description 建群
   * @param {string} userId
   * @returns
   */
  static async add (group_name, group_notice, group_avatar, group_creater) {
    return await models.chat_group_info.create({
      group_name,
      group_notice,
      group_avatar,
      group_creater,
      create_by: group_creater
    })
  }

  /**
   * @description 查找群信息
   * @param {string} group_id
   * @returns
   */
  static async find (group_id) {
    const sql = `select cgi.group_id, cgi.group_name, cgi.group_notice, cgi.group_avatar, cgi.group_creater, su.realname from chat_group_info cgi 
    left JOIN sys_user su ON cgi.group_creater = su.user_id WHERE cgi.group_id = ${group_id} and cgi.delete_timestamp is NULL and su.delete_timestamp is NULL`
    logger.info(`ChatGroupInfoModel`, `find`, `sql`, sql)
    const result = await models.sequelize.query(sql, {
      replacements: ['active'],
      type: models.sequelize.QueryTypes.SELECT
    })
    return result
  }

  /**
   * @description 查找群信息
   * @param {string} group_id
   * @param {string} group_name
   * @returns
   */
  static async findInfoByNameOrId (group_id, group_name) {
    const sql = `select cgi.group_id, cgi.group_name, cgi.group_notice, cgi.group_avatar, cgi.group_creater, su.realname from chat_group_info cgi 
    left JOIN sys_user su ON cgi.group_creater = su.user_id WHERE cgi.group_id = ${group_id} or cgi.group_name = ${group_name} and cgi.delete_timestamp is NULL and su.delete_timestamp is NULL`
    const result = await models.sequelize.query(sql, {
      replacements: ['active'],
      type: models.sequelize.QueryTypes.SELECT
    })
    return result
  }

  /**
   * @description 编辑群
   * @param {string} userId
   * @returns
   */
  static async edit (group_id, group_name, group_notice, group_avatar, user_id) {
    return await models.chat_group_info.update(
      {
        group_name,
        group_notice,
        group_avatar,
        update_by: user_id,
        update_timestamp: Date.now()
      },
      {
        where: { group_id }
      }
    )
  }

  /**
   * @description 获取群信息
   * @param {string} user_id
   * @returns
   */
  static async findGroup (user_id, group_name) {
    const sql = `SELECT
    group_id,group_name,group_avatar,su.realname as creater
  FROM
    chat_group_info cgi LEFT JOIN sys_user su on cgi.group_creater = su.user_id 
  WHERE
    cgi.group_id != ${user_id}
    AND cgi.group_name LIKE '%${group_name}%'
    AND cgi.delete_timestamp IS NULL`
    const result = await models.sequelize.query(sql, {
      replacements: ['active'],
      type: models.sequelize.QueryTypes.SELECT
    })
    return result
  }

  /**
   * @description 获取群信息
   * @param {string} user_id
   * @returns
   */
  static async findByName (group_name) {
    const sql = `SELECT
    group_id,group_name,group_avatar,su.realname as creater,su.user_id as creater_id
  FROM
    chat_group_info cgi LEFT JOIN sys_user su on cgi.group_creater = su.user_id 
  WHERE cgi.group_name LIKE '%${group_name}%'
    AND cgi.delete_timestamp IS NULL`
    const result = await models.sequelize.query(sql, {
      replacements: ['active'],
      type: models.sequelize.QueryTypes.SELECT
    })
    return result
  }
}

module.exports = {
  ChatGroupInfoModel
}
